package miao.byusi.bkata.logs.config

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.fasterxml.jackson.module.kotlin.readValue
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import java.io.InputStream

object ConfigLoader {
    private val mapper = ObjectMapper(YAMLFactory()).registerKotlinModule()
    
    fun loadConfig(inputStream: InputStream): BkataConfig {
        return try {
            mapper.readValue(inputStream)
        } catch (e: Exception) {
            // 配置加载失败时使用默认配置
            BkataConfig()
        }
    }
    
    fun loadDefaultConfig(): BkataConfig {
        return try {
            // 确保从正确的类加载器获取资源
            Thread.currentThread().contextClassLoader.getResourceAsStream("bkata-config.yml")?.use {
                loadConfig(it)
            } ?: BkataConfig()
        } catch (e: Exception) {
            BkataConfig()
        }
    }
}